<template>
    <div>
        <!-- 新增标签选择部分 -->
        <div class="mb-4 bg-white b-2 p-4">
            <label for="tags" class="mr-2">选择标签:</label>
            <select v-model="selectedTag" id="tags" class="p-1 border border-gray-300 rounded">
                <option value="生活">生活</option>
                <option value="娱乐">娱乐</option>
                <option value="学习">学习</option>
                <option value="新闻">新闻</option>
            </select>
        </div>
        <editor
            v-model="myValue"
            :init="init"
            api-key="w90em5a27zluxcvfiubyjvhpx8qqphbclxupve7rpi2dn66s"
            class="w-full h-full"
        ></editor>
        <!-- 新增底部按钮部分 -->
        <div class="flex justify-end mt-4">
            <!-- 原代码可能存在 VLS 识别问题，这里使用原生 HTML 按钮 -->
            <button @click="cancel" class="bg-gray-500 text-white px-4 py-2 rounded mr-2">取消</button>
            <button @click="save" class="bg-blue-500 text-white px-4 py-2 rounded">保存</button>
            <el-button>阿斯顿发生</el-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/icons/default/icons";
import "tinymce/models/dom"; // 一定要引入
import "tinymce/themes/silver"; // 界面UI主题
import "tinymce/plugins/image";
import "tinymce/plugins/table";
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/wordcount"; // 文字计数
import "tinymce/plugins/preview"; // 预览
import "tinymce/plugins/emoticons"; // emoji表情
import "tinymce/plugins/emoticons/js/emojis.js"; //必须引入这个文件才有表情图库
import "tinymce/plugins/code"; // 编辑源码
import "tinymce/plugins/link"; // 链接插件
import "tinymce/plugins/advlist"; //高级列表
import "tinymce/plugins/codesample"; //代码示例
import "tinymce/plugins/autoresize"; // 自动调整编辑器大小
import "tinymce/plugins/quickbars"; // 光标处快捷提示
import "tinymce/plugins/nonbreaking"; //插入不间断空格
import "tinymce/plugins/searchreplace"; //查找替换
import "tinymce/plugins/autolink"; //自动链接
import "tinymce/plugins/directionality"; //文字方向
import "tinymce/plugins/visualblocks"; //显示元素范围
import "tinymce/plugins/visualchars"; //显示不可见字符
import "tinymce/plugins/charmap"; // 特殊符号
import "tinymce/plugins/nonbreaking"; //插入不间断空格
import "tinymce/plugins/insertdatetime"; //插入日期时间
import "tinymce/plugins/importcss"; //引入自定义样式的css文件
import "tinymce/plugins/accordion"; // 可折叠数据手风琴模式
import "tinymce/plugins/anchor"; //锚点
import "tinymce/plugins/fullscreen";
import "tinymce/plugins/media"
import { onMounted, ref, reactive } from "vue";

onMounted(() => {
    tinymce.init({});
})
let runtime = (import.meta as any).env.VITE_RUNTIME; // 引入环境变量
//定义一个对象 init初始化
const init = reactive({
    language_url: runtime === 'development'? "/tinymce/langs/zh_CN.js": "./tinymce/langs/zh_CN.js", // 语言包的路径，具体路径看自己的项目
    language: "zh_CN",
    skin_url:  runtime === 'development'? "/tinymce/skins/ui/oxide" : "./tinymce/skins/ui/oxide" , // skin路径，具体路径看自己的项目
    height: 600,
    branding: false, // 是否禁用“Powered by TinyMCE”
    promotion: false, //去掉 upgrade
    // toolbar_sticky: true,
    // toolbar_sticky_offset: 100,
    menubar: "file edit view insert format tools table",  
    paste_data_images: true, //允许粘贴图像
    image_dimensions: false, //去除宽高属性
    plugins: "preview code nonbreaking importcss autoresize searchreplace autolink directionality code visualblocks visualchars fullscreen image media link codesample table charmap nonbreaking anchor insertdatetime advlist lists wordcount charmap quickbars emoticons accordion" , //这里的数据是在props里面就定义好了的
    toolbar: "preview | code | nonbreaking | undo redo | accordion accordionremove | blocks fontfamily fontsize| bold italic underline strikethrough ltr rtl  | align numlist bullist | link image media | table | lineheight outdent indent| forecolor backcolor removeformat | charmap emoticons | anchor codesample", //这里的数据是在props里面就定义好了的
    // 取消图片资源路径转换
    convert_urls: false,
    // table边框位0是否展示网格线
    // visual: false,
    // 超链接默认打开方式
    link_default_target: "_blank",
    link_context_toolbar: true,
    // 默认快捷菜单
    quickbars_insert_toolbar: "image codesample table code pagebreak",
    // 选中图片的快捷提示
    quickbars_image_toolbar: "alignleft aligncenter alignright | rotateleft rotateright | imageoptions",
    editimage_toolbar: "rotateleft rotateright | flipv fliph | editimage imageoptions",
    // 文字样式
    font_family_formats:
        "Arial=arial,helvetica,sans-serif; 宋体=SimSun; 微软雅黑=Microsoft Yahei; Impact=impact,chicago;", //字体
    font_size_formats: "11px 12px 14px 16px 18px 24px 36px 48px 64px 72px", //文字大小
    image_caption: true,
    editimage_cors_hosts: ["picsum.photos"],
    noneditable_class: "mceNonEditable",
    toolbar_mode: "wrap", // 工具栏模式 floating / sliding / scrolling / wrap
    // 默认样式
    content_style:
        "body { font-family:Helvetica,Arial,sans-serif; font-size:16px }p {margin:3px; line-height:24px;}",
    image_advtab: true,
    importcss_append: true,
    paste_webkit_styles: "all",
    paste_merge_formats: true,
    nonbreaking_force_tab: false,
    paste_auto_cleanup_on_paste: false,
    file_picker_types: "media",
    media_live_embeds: true,
    images_reuse_filename: true,
    // 选中文字的快捷提示
    quickbars_selection_toolbar:
        "bold italic | quicklink h2 h3 blockquote quickimage quicktable",
    // 编辑器高度自适应
    autoresize_bottom_margin: 20,
    // autoresize_overflow_padding: 16,
    min_height: 600,
    content_css: runtime === 'development'? "/tinymce/skins/content/default/content.css" : "./tinymce/skins/content/default/content.css", //以css文件方式自定义可编辑区域的css样式，css文件需自己创建并引入
});

// 外部传递进来的数据变化
const myValue = ref("");
// 新增：选中的标签
const selectedTag = ref("生活");

// 新增：保存和取消方法
const save = () => {
    // 这里可以添加保存日记的逻辑，例如发送请求到后端
    console.log("保存日记，内容：", myValue.value, "标签：", selectedTag.value);
};

const cancel = () => {
    // 这里可以添加取消操作的逻辑，例如清空编辑器内容
    myValue.value = "";
    selectedTag.value = "生活";
    console.log("取消操作");
};
</script>
